home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / complete.el < prev    next >
Lisp/Scheme  |  1994-08-07  |  29KB  |  874 lines

  1. ;; complete.el -- partial completion mechanism plus other goodies.
  2.  
  3. ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Dave Gillespie <daveg@synaptics.com>
  6. ;; Version: 2.02
  7. ;; Special thanks to Hallvard Furuseth for his many ideas and contributions.
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Extended completion for the Emacs minibuffer.
  28. ;;
  29. ;; The basic idea is that the command name or other completable text is
  30. ;; divided into words and each word is completed separately, so that
  31. ;; "M-x p-b" expands to "M-x print-buffer".  If the entry is ambiguous
  32. ;; each word is completed as much as possible and then the cursor is
  33. ;; left at the first position where typing another letter will resolve
  34. ;; the ambiguity.
  35. ;;
  36. ;; Word separators for this purpose are hyphen, space, and period.
  37. ;; These would most likely occur in command names, Info menu items,
  38. ;; and file names, respectively.  But all word separators are treated
  39. ;; alike at all times.
  40. ;;
  41. ;; This completion package replaces the old-style completer's key
  42. ;; bindings for TAB, SPC, RET, and `?'.  The old completer is still
  43. ;; available on the Meta versions of those keys.  If you set
  44. ;; PC-meta-flag to nil, the old completion keys will be left alone
  45. ;; and the partial completer will use the Meta versions of the keys.
  46.  
  47.  
  48. ;; Usage:  Load this file.  Now, during completable minibuffer entry,
  49. ;;
  50. ;;     TAB    means to do a partial completion;
  51. ;;     SPC    means to do a partial complete-word;
  52. ;;     RET    means to do a partial complete-and-exit;
  53. ;;     ?      means to do a partial completion-help.
  54. ;;
  55. ;; If you set PC-meta-flag to nil, then TAB, SPC, RET, and ? perform
  56. ;; original Emacs completions, and M-TAB etc. do partial completion.
  57. ;; To do this, put the command,
  58. ;;
  59. ;;       (setq PC-meta-flag nil)
  60. ;;
  61. ;; in your .emacs file.  To load partial completion automatically, put
  62. ;;
  63. ;;       (load "complete")
  64. ;;
  65. ;; in your .emacs file, too.  Things will be faster if you byte-compile
  66. ;; this file when you install it.
  67. ;;
  68. ;; As an extra feature, in cases where RET would not normally
  69. ;; complete (such as `C-x b'), the M-RET key will always do a partial
  70. ;; complete-and-exit.  Thus `C-x b f.c RET' will select or create a
  71. ;; buffer called "f.c", but `C-x b f.c M-RET' will select the existing
  72. ;; buffer whose name matches that pattern (perhaps "filing.c").
  73. ;; (PC-meta-flag does not affect this behavior; M-RET used to be
  74. ;; undefined in this situation.)
  75. ;;
  76. ;; The regular M-TAB (lisp-complete-symbol) command also supports
  77. ;; partial completion in this package.
  78.  
  79. ;; This package also contains a wildcard feature for C-x C-f (find-file).
  80. ;; For example, `C-x C-f *.c RET' loads all .c files at once, exactly
  81. ;; as if you had typed C-x C-f separately for each file.  Completion
  82. ;; is supported in connection with wildcards.  Currently only the `*'
  83. ;; wildcard character works.
  84.  
  85. ;; File name completion does not do partial completion of directories
  86. ;; on the path, e.g., "/u/b/f" will not complete to "/usr/bin/foo",
  87. ;; but you can put *'s in the path to accomplish this:  "/u*/b*/f".
  88. ;; Stars are required for performance reasons.
  89.  
  90. ;; In addition, this package includes a feature for accessing include
  91. ;; files.  For example, `C-x C-f <sys/time.h> RET' reads the file
  92. ;; /usr/include/sys/time.h.  The variable PC-include-file-path is a
  93. ;; list of directories in which to search for include files.  Completion
  94. ;; is supported in include file names.
  95.  
  96.  
  97. ;;; Code:
  98.  
  99. (defvar PC-meta-flag t
  100.   "*If nil, TAB does normal Emacs completion and M-TAB does Partial Completion.
  101. If t, TAB does Partial Completion and M-TAB does normal completion.")
  102.  
  103.  
  104. (defvar PC-word-delimiters "-_. "
  105.   "*A string of characters which are to be treated as word delimiters
  106. by the Partial Completion system.
  107.  
  108. Some arcane rules:  If `]' is in this string it must come first.
  109. If `^' is in this string it must NOT come first.  If `-' is in this
  110. string, it must come first or right after `]'.  In other words, if
  111. S is this string, then `[S]' must be a legal Emacs regular expression
  112. \(not containing character ranges like `a-z').")
  113.  
  114.  
  115. (defvar PC-first-char 'x
  116.   "*If t, first character of a string to be completed is always taken literally.
  117. If nil, word delimiters are handled even if they appear as first character.
  118. This controls whether \".e\" matches \".e*\" (t) or \"*.e*\" (nil).
  119. If neither nil nor t, first char is literal only for filename completion.")
  120.  
  121.  
  122. (defvar PC-include-file-path '("/usr/include")
  123.   "*List of directories in which to look for include files.
  124. If this is nil, uses the colon-separated path in $INCPATH instead.")
  125.  
  126.  
  127. (defvar PC-disable-wildcards nil
  128.   "Set this to non-nil to disable wildcard support in \\[find-file].")
  129.  
  130. (defvar PC-disable-includes nil
  131.   "Set this to non-nil to disable include-file support in \\[find-file].")
  132.  
  133.  
  134. (defvar PC-default-bindings t
  135.   "Set this to nil to suppress the default partial completion key bindings.")
  136.  
  137. (if PC-default-bindings (progn
  138. (define-key minibuffer-local-completion-map "\t" 'PC-complete)
  139. (define-key minibuffer-local-completion-map " "  'PC-complete-word)
  140. (define-key minibuffer-local-completion-map "?"  'PC-completion-help)
  141.  
  142. (define-key minibuffer-local-completion-map "\e\t" 'PC-complete)
  143. (define-key minibuffer-local-completion-map "\e "  'PC-complete-word)
  144. (define-key minibuffer-local-completion-map "\e\r" 'PC-force-complete-and-exit)
  145. (define-key minibuffer-local-completion-map "\e\n" 'PC-force-complete-and-exit)
  146. (define-key minibuffer-local-completion-map "\e?"  'PC-completion-help)
  147.  
  148. (define-key minibuffer-local-must-match-map "\t" 'PC-complete)
  149. (define-key minibuffer-local-must-match-map " "  'PC-complete-word)
  150. (define-key minibuffer-local-must-match-map "\r" 'PC-complete-and-exit)
  151. (define-key minibuffer-local-must-match-map "\n" 'PC-complete-and-exit)
  152. (define-key minibuffer-local-must-match-map "?"  'PC-completion-help)
  153.  
  154. (define-key minibuffer-local-must-match-map "\e\t" 'PC-complete)
  155. (define-key minibuffer-local-must-match-map "\e "  'PC-complete-word)
  156. (define-key minibuffer-local-must-match-map "\e\r" 'PC-complete-and-exit)
  157. (define-key minibuffer-local-must-match-map "\e\n" 'PC-complete-and-exit)
  158. (define-key minibuffer-local-must-match-map "\e?"  'PC-completion-help)
  159.  
  160. (define-key global-map "\e\t" 'PC-lisp-complete-symbol)
  161. ))
  162.  
  163.  
  164. (defun PC-complete ()
  165.   "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
  166. For example, \"M-x b--di\" would match `byte-recompile-directory', or any
  167. name which consists of three or more words, the first beginning with \"b\"
  168. and the third beginning with \"di\".
  169.  
  170. The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
  171. `beginning-of-defun', so this would produce a list of completions
  172. just like when normal Emacs completions are ambiguous.
  173.  
  174. Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
  175. \".\", and SPC."
  176.   (interactive)
  177.   (if (PC-was-meta-key)
  178.       (minibuffer-complete)
  179.     (PC-do-completion nil)))
  180.  
  181.  
  182. (defun PC-complete-word ()
  183.   "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
  184. See `PC-complete' for details.
  185. This can be bound to other keys, like `-' and `.', if you wish."
  186.   (interactive)
  187.   (if (eq (PC-was-meta-key) PC-meta-flag)
  188.       (if (eq last-command-char ? )
  189.       (minibuffer-complete-word)
  190.     (self-insert-command 1))
  191.     (self-insert-command 1)
  192.     (if (eobp)
  193.     (PC-do-completion 'word))))
  194.  
  195.  
  196. (defun PC-complete-space ()
  197.   "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
  198. See `PC-complete' for details.
  199. This is suitable for binding to other keys which should act just like SPC."
  200.   (interactive)
  201.   (if (eq (PC-was-meta-key) PC-meta-flag)
  202.       (minibuffer-complete-word)
  203.     (insert " ")
  204.     (if (eobp)
  205.     (PC-do-completion 'word))))
  206.  
  207.  
  208. (defun PC-complete-and-exit ()
  209.   "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
  210. See `PC-complete' for details."
  211.   (interactive)
  212.   (if (eq (PC-was-meta-key) PC-meta-flag)
  213.       (minibuffer-complete-and-exit)
  214.     (PC-do-complete-and-exit)))
  215.  
  216. (defun PC-force-complete-and-exit ()
  217.   "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
  218. See `PC-complete' for details."
  219.   (interactive)
  220.   (let ((minibuffer-completion-confirm nil))
  221.     (PC-do-complete-and-exit)))
  222.  
  223. (defun PC-do-complete-and-exit ()
  224.   (if (= (buffer-size) 0)  ; Duplicate the "bug" that Info-menu relies on...
  225.       (exit-minibuffer)
  226.     (let ((flag (PC-do-completion 'exit)))
  227.       (and flag
  228.        (if (or (eq flag 'complete)
  229.            (not minibuffer-completion-confirm))
  230.            (exit-minibuffer)
  231.          (PC-temp-minibuffer-message " (Confirm)"))))))
  232.  
  233.  
  234. (defun PC-completion-help ()
  235.   "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
  236. See `PC-complete' for details."
  237.   (interactive)
  238.   (if (eq (PC-was-meta-key) PC-meta-flag)
  239.       (minibuffer-completion-help)
  240.     (PC-do-completion 'help)))
  241.  
  242. (defun PC-was-meta-key ()
  243.   (or (/= (length (this-command-keys)) 1)
  244.       (let ((key (aref (this-command-keys) 0)))
  245.     (if (integerp key)
  246.         (>= key 128)
  247.       (not (null (memq 'meta (event-modifiers key))))))))
  248.  
  249.  
  250. (defvar PC-ignored-extensions 'empty-cache)
  251. (defvar PC-delims 'empty-cache)
  252. (defvar PC-ignored-regexp nil)
  253. (defvar PC-word-failed-flag nil)
  254. (defvar PC-delim-regex nil)
  255. (defvar PC-ndelims-regex nil)
  256. (defvar PC-delims-list nil)
  257.  
  258. (defun PC-do-completion (&optional mode beg end)
  259.   (or beg (setq beg (point-min)))
  260.   (or end (setq end (point-max)))
  261.   (let* ((table minibuffer-completion-table)
  262.      (pred minibuffer-completion-predicate)
  263.      (filename (memq table '(read-file-name-internal
  264.                  read-directory-name-internal)))
  265.      (dirname nil)
  266.      dirlength
  267.      (str (buffer-substring beg end))
  268.      (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
  269.      (ambig nil)
  270.      basestr
  271.      regex
  272.      p offset
  273.      (poss nil)
  274.      helpposs
  275.      (case-fold-search completion-ignore-case))
  276.  
  277.     ;; Check if buffer contents can already be considered complete
  278.     (if (and (eq mode 'exit)
  279.          (PC-is-complete-p str table pred))
  280.     'complete
  281.  
  282.       ;; Record how many characters at the beginning are not included
  283.       ;; in completion.
  284.       (setq dirlength
  285.         (if filename
  286.         (length (file-name-directory str))
  287.           0))
  288.  
  289.       ;; Do substitutions in directory names
  290.       (and filename
  291.        (not (equal str (setq p (substitute-in-file-name str))))
  292.        (progn
  293.          (delete-region beg end)
  294.          (insert p)
  295.          (setq str p end (+ beg (length str)))))
  296.  
  297.       ;; Prepare various delimiter strings
  298.       (or (equal PC-word-delimiters PC-delims)
  299.       (setq PC-delims PC-word-delimiters
  300.         PC-delim-regex (concat "[" PC-delims "]")
  301.         PC-ndelims-regex (concat "[^" PC-delims "]*")
  302.         PC-delims-list (append PC-delims nil)))
  303.  
  304.       ;; Look for wildcard expansions in directory name
  305.       (and filename
  306.        (string-match "\\*.*/" str)
  307.        (let ((pat str)
  308.          files)
  309.          (setq p (1+ (string-match "/[^/]*\\'" pat)))
  310.          (while (setq p (string-match PC-delim-regex pat p))
  311.            (setq pat (concat (substring pat 0 p)
  312.                  "*"
  313.                  (substring pat p))
  314.              p (+ p 2)))
  315.          (setq files (PC-expand-many-files (concat pat "*")))
  316.          (if files
  317.          (let ((dir (file-name-directory (car files)))
  318.                (p files))
  319.            (while (and (setq p (cdr p))
  320.                    (equal dir (file-name-directory (car p)))))
  321.            (if p
  322.                (setq filename nil table nil pred nil
  323.                  ambig t)
  324.              (delete-region beg end)
  325.              (setq str (concat dir (file-name-nondirectory str)))
  326.              (insert str)
  327.              (setq end (+ beg (length str)))))
  328.            (setq filename nil table nil pred nil))))
  329.  
  330.       ;; Strip directory name if appropriate
  331.       (if filename
  332.       (if incname
  333.           (setq basestr (substring str incname)
  334.             dirname (substring str 0 incname))
  335.         (setq basestr (file-name-nondirectory str)
  336.           dirname (file-name-directory str)))
  337.     (setq basestr str))
  338.  
  339.       ;; Convert search pattern to a standard regular expression
  340.       (setq regex (regexp-quote basestr)
  341.         offset (if (and (> (length regex) 0)
  342.                 (not (eq (aref basestr 0) ?\*))
  343.                 (or (eq PC-first-char t)
  344.                 (and PC-first-char filename))) 1 0)
  345.         p offset)
  346.       (while (setq p (string-match PC-delim-regex regex p))
  347.     (if (eq (aref regex p) ? )
  348.         (setq regex (concat (substring regex 0 p)
  349.                 PC-ndelims-regex
  350.                 PC-delim-regex
  351.                 (substring regex (1+ p)))
  352.           p (+ p (length PC-ndelims-regex) (length PC-delim-regex)))
  353.       (let ((bump (if (memq (aref regex p)
  354.                 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
  355.               -1 0)))
  356.         (setq regex (concat (substring regex 0 (+ p bump))
  357.                 PC-ndelims-regex
  358.                 (substring regex (+ p bump)))
  359.           p (+ p (length PC-ndelims-regex) 1)))))
  360.       (setq p 0)
  361.       (if filename
  362.       (while (setq p (string-match "\\\\\\*" regex p))
  363.         (setq regex (concat (substring regex 0 p)
  364.                 "[^/]*"
  365.                 (substring regex (+ p 2))))))
  366.       ;;(setq the-regex regex)
  367.       (setq regex (concat "\\`" regex))
  368.  
  369.       ;; Find an initial list of possible completions
  370.       (if (not (setq p (string-match (concat PC-delim-regex
  371.                          (if filename "\\|\\*" ""))
  372.                      str
  373.                      (+ (length dirname) offset))))
  374.  
  375.       ;; Minibuffer contains no hyphens -- simple case!
  376.       (setq poss (all-completions str
  377.                       table
  378.                       pred))
  379.  
  380.     ;; Use all-completions to do an initial cull.  This is a big win,
  381.     ;; since all-completions is written in C!
  382.     (let ((compl (all-completions (substring str 0 p)
  383.                       table
  384.                       pred)))
  385.       (setq p compl)
  386.       (while p
  387.         (and (string-match regex (car p))
  388.          (setq poss (cons (car p) poss)))
  389.         (setq p (cdr p)))))
  390.  
  391.       ;; Now we have a list of possible completions
  392.       (cond
  393.  
  394.        ;; No valid completions found
  395.        ((null poss)
  396.     (if (and (eq mode 'word)
  397.          (not PC-word-failed-flag))
  398.         (let ((PC-word-failed-flag t))
  399.           (delete-backward-char 1)
  400.           (PC-do-completion 'word))
  401.       (beep)
  402.       (PC-temp-minibuffer-message (if ambig
  403.                       " (Ambiguous dir name)"
  404.                     (if (eq mode 'help)
  405.                         " (No completions)"
  406.                       " (No match)")))
  407.       nil))
  408.  
  409.        ;; More than one valid completion found
  410.        ((or (cdr (setq helpposs poss))
  411.         (memq mode '(help word)))
  412.  
  413.     ;; Handle completion-ignored-extensions
  414.     (and filename
  415.          (not (eq mode 'help))
  416.          (let ((p2 poss))
  417.  
  418.            ;; Build a regular expression representing the extensions list
  419.            (or (equal completion-ignored-extensions PC-ignored-extensions)
  420.            (setq PC-ignored-regexp
  421.              (concat "\\("
  422.                  (mapconcat
  423.                   'regexp-quote
  424.                   (setq PC-ignored-extensions
  425.                     completion-ignored-extensions)
  426.                   "\\|")
  427.                  "\\)\\'")))
  428.  
  429.            ;; Check if there are any without an ignored extension
  430.            (setq p nil)
  431.            (while p2
  432.          (or (string-match PC-ignored-regexp (car p2))
  433.              (setq p (cons (car p2) p)))
  434.          (setq p2 (cdr p2)))
  435.  
  436.            ;; If there are "good" names, use them
  437.            (and p (setq poss p))))
  438.  
  439.     ;; Is the actual string one of the possible completions?
  440.     (setq p (and (not (eq mode 'help)) poss))
  441.     (while (and p
  442.             (not (equal (car p) basestr)))
  443.       (setq p (cdr p)))
  444.     (if p
  445.  
  446.         (progn
  447.           (if (null mode)
  448.           (PC-temp-minibuffer-message " (Complete, but not unique)"))
  449.           t)
  450.  
  451.       ;; If ambiguous, try for a partial completion
  452.       (let ((improved nil)
  453.         prefix
  454.         (pt nil)
  455.         (skip "\\`"))
  456.  
  457.         ;; Check if next few letters are the same in all cases
  458.         (if (and (not (eq mode 'help))
  459.              (setq prefix (try-completion "" (mapcar 'list poss))))
  460.         (let ((first t) i)
  461.           (if (eq mode 'word)
  462.               (setq prefix (PC-chop-word prefix basestr)))
  463.           (goto-char (+ beg (length dirname)))
  464.           (while (and (progn
  465.                 (setq i 0)
  466.                 (while (< i (length prefix))
  467.                   (if (and (< (point) end)
  468.                        (eq (aref prefix i)
  469.                            (following-char)))
  470.                       (forward-char 1)
  471.                     (if (and (< (point) end)
  472.                          (or (and (looking-at " ")
  473.                               (memq (aref prefix i)
  474.                                 PC-delims-list))
  475.                          (eq (downcase (aref prefix i))
  476.                              (downcase
  477.                               (following-char)))))
  478.                     (progn
  479.                       (delete-char 1)
  480.                       (setq end (1- end)))
  481.                       (and filename (looking-at "\\*")
  482.                        (progn
  483.                          (delete-char 1)
  484.                          (setq end (1- end))))
  485.                       (setq improved t))
  486.                     ;; Use format to discard text properties.
  487.                     (insert (format "%s" (substring prefix i (1+ i))))
  488.                     (setq end (1+ end)))
  489.                   (setq i (1+ i)))
  490.                 (or pt (equal (point) beg)
  491.                     (setq pt (point)))
  492.                 (looking-at PC-delim-regex))
  493.                   (setq skip (concat skip
  494.                          (regexp-quote prefix)
  495.                          PC-ndelims-regex)
  496.                     prefix (try-completion
  497.                         ""
  498.                         (mapcar
  499.                          (function
  500.                           (lambda (x)
  501.                         (list
  502.                          (and (string-match skip x)
  503.                               (substring
  504.                                x
  505.                                (match-end 0))))))
  506.                          poss)))
  507.                   (or (> i 0) (> (length prefix) 0))
  508.                   (or (not (eq mode 'word))
  509.                   (and first (> (length prefix) 0)
  510.                        (setq first nil
  511.                          prefix (substring prefix 0 1))))))
  512.           (goto-char (if (eq mode 'word) end
  513.                    (or pt beg)))))
  514.  
  515.         (if (and (eq mode 'word)
  516.              (not PC-word-failed-flag))
  517.  
  518.         (if improved
  519.  
  520.             ;; We changed it... would it be complete without the space?
  521.             (if (PC-is-complete-p (buffer-substring 1 (1- end))
  522.                       table pred)
  523.             (delete-region (1- end) end)))
  524.  
  525.           (if improved
  526.  
  527.           ;; We changed it... enough to be complete?
  528.           (and (eq mode 'exit)
  529.                (PC-is-complete-p (buffer-string) table pred))
  530.  
  531.         ;; If totally ambiguous, display a list of completions
  532.         (if (or completion-auto-help
  533.             (eq mode 'help))
  534.             (with-output-to-temp-buffer "*Completions*"
  535.               (display-completion-list (sort helpposs 'string-lessp))
  536.               (save-excursion
  537.             (set-buffer standard-output)
  538.             ;; Record which part of the buffer we are completing
  539.             ;; so that choosing a completion from the list
  540.             ;; knows how much old text to replace.
  541.             (setq completion-base-size dirlength)))
  542.           (PC-temp-minibuffer-message " (Next char not unique)"))
  543.         nil)))))
  544.  
  545.        ;; Only one possible completion
  546.        (t
  547.     (if (equal basestr (car poss))
  548.         (if (null mode)
  549.         (PC-temp-minibuffer-message " (Sole completion)"))
  550.       (delete-region beg end)
  551.       (insert (format "%s"
  552.               (if filename
  553.                   (substitute-in-file-name (concat dirname (car poss)))
  554.                 (car poss)))))
  555.     t)))))
  556.  
  557.  
  558. (defun PC-is-complete-p (str table pred)
  559.   (let ((res (if (listp table)
  560.          (assoc str table)
  561.            (if (vectorp table)
  562.            (or (equal str "nil")   ; heh, heh, heh
  563.                (intern-soft str table))
  564.          (funcall table str pred 'lambda)))))
  565.     (and res
  566.      (or (not pred)
  567.          (and (not (listp table)) (not (vectorp table)))
  568.          (funcall pred res))
  569.      res)))
  570.  
  571. (defun PC-chop-word (new old)
  572.   (let ((i -1)
  573.     (j -1))
  574.     (while (and (setq i (string-match PC-delim-regex old (1+ i)))
  575.         (setq j (string-match PC-delim-regex new (1+ j)))))
  576.     (if (and j
  577.          (or (not PC-word-failed-flag)
  578.          (setq j (string-match PC-delim-regex new (1+ j)))))
  579.     (substring new 0 (1+ j))
  580.       new)))
  581.  
  582. (defvar PC-not-minibuffer nil)
  583.  
  584. (defun PC-temp-minibuffer-message (m)
  585.   "A Lisp version of `temp_minibuffer_message' from minibuf.c."
  586.   (if PC-not-minibuffer
  587.       (progn
  588.     (message m)
  589.     (sit-for 2)
  590.     (message ""))
  591.     (if (fboundp 'temp-minibuffer-message)
  592.     (temp-minibuffer-message m)
  593.       (let ((savemax (point-max)))
  594.     (save-excursion
  595.       (goto-char (point-max))
  596.       (insert m))
  597.     (let ((inhibit-quit t))
  598.       (sit-for 2)
  599.       (delete-region savemax (point-max))
  600.       (if quit-flag
  601.           (setq quit-flag nil
  602.             unread-command-char 7)))))))
  603.  
  604.  
  605. (defun PC-lisp-complete-symbol ()
  606.   "Perform completion on Lisp symbol preceding point.
  607. That symbol is compared against the symbols that exist
  608. and any additional characters determined by what is there
  609. are inserted.
  610. If the symbol starts just after an open-parenthesis,
  611. only symbols with function definitions are considered.
  612. Otherwise, all symbols with function definitions, values
  613. or properties are considered."
  614.   (interactive)
  615.   (let* ((end (point))
  616.      (buffer-syntax (syntax-table))
  617.      (beg (unwind-protect
  618.           (save-excursion
  619.             (if lisp-mode-syntax-table
  620.             (set-syntax-table lisp-mode-syntax-table))
  621.             (backward-sexp 1)
  622.             (while (= (char-syntax (following-char)) ?\')
  623.               (forward-char 1))
  624.             (point))
  625.         (set-syntax-table buffer-syntax)))
  626.      (minibuffer-completion-table obarray)
  627.      (minibuffer-completion-predicate
  628.       (if (eq (char-after (1- beg)) ?\()
  629.           'fboundp
  630.         (function (lambda (sym)
  631.             (or (boundp sym) (fboundp sym)
  632.                 (symbol-plist sym))))))
  633.      (PC-not-minibuffer t))
  634.     (PC-do-completion nil beg end)))
  635.  
  636.  
  637. ;;; Wildcards in `C-x C-f' command.  This is independent from the main
  638. ;;; completion code, except for `PC-expand-many-files' which is called
  639. ;;; when "*"'s are found in the path during filename completion.  (The
  640. ;;; above completion code always understands "*"'s, except in file paths,
  641. ;;; without relying on the following code.)
  642.  
  643. (defvar PC-many-files-list nil)
  644.  
  645. (defun PC-try-load-many-files ()
  646.   (if (string-match "\\*" buffer-file-name)
  647.       (let* ((pat buffer-file-name)
  648.          (files (PC-expand-many-files pat))
  649.          (first (car files))
  650.          (next files))
  651.     (kill-buffer (current-buffer))
  652.     (or files
  653.         (error "No matching files"))
  654.     (save-window-excursion
  655.       (while (setq next (cdr next))
  656.         (let ((buf (find-file-noselect (car next))))
  657.           (switch-to-buffer buf))))
  658.     ;; This modifies the "buf" variable inside find-file-noselect.
  659.     (setq buf (get-file-buffer first))
  660.     (if buf
  661.         nil   ; should do verify-visited-file-modtime stuff.
  662.       (setq filename first)
  663.       (setq buf (create-file-buffer filename))
  664.       (set-buffer buf)
  665.       (erase-buffer)
  666.       (insert-file-contents filename t))
  667.     (if (cdr files)
  668.         (setq PC-many-files-list (mapconcat
  669.                       (if (string-match "\\*.*/" pat)
  670.                       'identity
  671.                     'file-name-nondirectory)
  672.                       (cdr files) ", ")
  673.           find-file-hooks (cons 'PC-after-load-many-files
  674.                     find-file-hooks)))
  675.     ;; This modifies the "error" variable inside find-file-noselect.
  676.     (setq error nil)
  677.     t)
  678.     nil))
  679.  
  680. (defun PC-after-load-many-files ()
  681.   (setq find-file-hooks (delq 'PC-after-load-many-files find-file-hooks))
  682.   (message "Also loaded %s." PC-many-files-list))
  683.  
  684. (defun PC-expand-many-files (name)
  685.   (save-excursion
  686.     (set-buffer (generate-new-buffer " *Glob Output*"))
  687.     (erase-buffer)
  688.     (shell-command (concat "echo " name) t)
  689.     (goto-char (point-min))
  690.     (if (looking-at ".*No match")
  691.     nil
  692.       (insert "(\"")
  693.       (while (search-forward " " nil t)
  694.     (delete-backward-char 1)
  695.     (insert "\" \""))
  696.       (goto-char (point-max))
  697.       (delete-backward-char 1)
  698.       (insert "\")")
  699.       (goto-char (point-min))
  700.       (let ((files (read (current-buffer))))
  701.     (kill-buffer (current-buffer))
  702.     files))))
  703.  
  704. (or PC-disable-wildcards
  705.     (memq 'PC-try-load-many-files find-file-not-found-hooks)
  706.     (setq find-file-not-found-hooks (cons 'PC-try-load-many-files
  707.                       find-file-not-found-hooks)))
  708.  
  709.  
  710.  
  711. ;;; Facilities for loading C header files.  This is independent from the
  712. ;;; main completion code.  See also the variable `PC-include-file-path'
  713. ;;; at top of this file.
  714.  
  715. (defun PC-look-for-include-file ()
  716.   (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
  717.       (let ((name (substring (buffer-file-name)
  718.                  (match-beginning 1) (match-end 1)))
  719.         (punc (aref (buffer-file-name) (match-beginning 0)))
  720.         (path nil)
  721.         new-buf)
  722.     (kill-buffer (current-buffer))
  723.     (if (equal name "")
  724.         (save-excursion
  725.           (set-buffer (car (buffer-list)))
  726.           (save-excursion
  727.         (beginning-of-line)
  728.         (if (looking-at
  729.              "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
  730.             (setq name (buffer-substring (match-beginning 1)
  731.                          (match-end 1))
  732.               punc (char-after (1- (match-beginning 1))))
  733.           ;; Suggested by Frank Siebenlist:
  734.           (if (or (looking-at
  735.                "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
  736.               (looking-at
  737.                "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
  738.               (looking-at
  739.                "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
  740.               (progn
  741.             (setq name (buffer-substring (match-beginning 1)
  742.                              (match-end 1))
  743.                   punc ?\<
  744.                   path load-path)
  745.             (if (string-match "\\.elc$" name)
  746.                 (setq name (substring name 0 -1))
  747.               (or (string-match "\\.el$" name)
  748.                   (setq name (concat name ".el")))))
  749.             (error "Not on an #include line"))))))
  750.     (or (string-match "\\.[a-zA-Z0-9]+$" name)
  751.         (setq name (concat name ".h")))
  752.     (if (eq punc ?\<)
  753.         (let ((path (or path (PC-include-file-path))))
  754.           (while (and path
  755.               (not (file-exists-p
  756.                 (concat (file-name-as-directory (car path))
  757.                     name))))
  758.         (setq path (cdr path)))
  759.           (if path
  760.           (setq name (concat (file-name-as-directory (car path)) name))
  761.         (error "No such include file: <%s>" name)))
  762.       (let ((dir (save-excursion
  763.                (set-buffer (car (buffer-list)))
  764.                default-directory)))
  765.         (if (file-exists-p (concat dir name))
  766.         (setq name (concat dir name))
  767.           (error "No such include file: \"%s\"" name))))
  768.     (setq new-buf (get-file-buffer name))
  769.     (if new-buf
  770.         ;; no need to verify last-modified time for this!
  771.         (set-buffer new-buf)
  772.       (setq new-buf (create-file-buffer name))
  773.       (set-buffer new-buf)
  774.       (erase-buffer)
  775.       (insert-file-contents name t))
  776.     (setq filename name
  777.           error nil
  778.           buf new-buf)
  779.     t)
  780.     nil))
  781.  
  782. (defun PC-include-file-path ()
  783.   (or PC-include-file-path
  784.       (let ((env (getenv "INCPATH"))
  785.         (path nil)
  786.         pos)
  787.     (or env (error "No include file path specified"))
  788.     (while (setq pos (string-match ":[^:]+$" env))
  789.       (setq path (cons (substring env (1+ pos)) path)
  790.         env (substring env 0 pos)))
  791.     path)))
  792.  
  793. ;;; This is adapted from lib-complete.el, by Mike Williams.
  794. (defun PC-include-file-all-completions (file search-path &optional full)
  795.   "Return all completions for FILE in any directory on SEARCH-PATH.
  796. If optional third argument FULL is non-nil, returned pathnames should be 
  797. absolute rather than relative to some directory on the SEARCH-PATH."
  798.   (setq search-path
  799.     (mapcar '(lambda (dir)
  800.            (if dir (file-name-as-directory dir) default-directory))
  801.         search-path))
  802.   (if (file-name-absolute-p file)
  803.       ;; It's an absolute file name, so don't need search-path
  804.       (progn
  805.     (setq file (expand-file-name file))
  806.     (file-name-all-completions 
  807.      (file-name-nondirectory file) (file-name-directory file)))
  808.     (let ((subdir (file-name-directory file))
  809.       (ndfile (file-name-nondirectory file))
  810.       file-lists)
  811.       ;; Append subdirectory part to each element of search-path
  812.       (if subdir
  813.       (setq search-path
  814.         (mapcar '(lambda (dir) (concat dir subdir))
  815.             search-path)
  816.         file ))
  817.       ;; Make list of completions in each directory on search-path
  818.       (while search-path
  819.     (let* ((dir (car search-path))
  820.            (subdir (if full dir subdir)))
  821.       (if (file-directory-p dir)
  822.           (progn
  823.         (setq file-lists
  824.               (cons 
  825.                (mapcar '(lambda (file) (concat subdir file))
  826.                    (file-name-all-completions ndfile 
  827.                               (car search-path)))
  828.                file-lists))))
  829.       (setq search-path (cdr search-path))))
  830.       ;; Compress out duplicates while building complete list (slloooow!)
  831.       (let ((sorted (sort (apply 'nconc file-lists)
  832.               '(lambda (x y) (not (string-lessp x y)))))
  833.         compressed)
  834.     (while sorted
  835.       (if (equal (car sorted) (car compressed)) nil
  836.         (setq compressed (cons (car sorted) compressed)))
  837.       (setq sorted (cdr sorted)))
  838.     compressed))))
  839.  
  840. (defvar PC-old-read-file-name-internal nil)
  841.  
  842. (defun PC-read-include-file-name-internal (string dir action)
  843.   (if (string-match "<\\([^\"<>]*\\)>?$" string)
  844.       (let* ((name (substring string (match-beginning 1) (match-end 1)))
  845.          (str2 (substring string (match-beginning 0)))
  846.          (completion-table
  847.           (mapcar (function (lambda (x) (list (format "<%s>" x))))
  848.               (PC-include-file-all-completions
  849.                name (PC-include-file-path)))))
  850.     (cond
  851.      ((not completion-table) nil)
  852.      ((eq action nil) (try-completion str2 completion-table nil))
  853.      ((eq action t) (all-completions str2 completion-table nil))
  854.      ((eq action 'lambda)
  855.       (eq (try-completion str2 completion-table nil) t))))
  856.     (funcall PC-old-read-file-name-internal string dir action)))
  857.  
  858. (or PC-disable-includes
  859.     (memq 'PC-look-for-include-file find-file-not-found-hooks)
  860.     (setq find-file-not-found-hooks (cons 'PC-look-for-include-file
  861.                       find-file-not-found-hooks)))
  862.  
  863. (or PC-disable-includes
  864.     PC-old-read-file-name-internal
  865.     (progn
  866.       (setq PC-old-read-file-name-internal
  867.         (symbol-function 'read-file-name-internal))
  868.       (fset 'read-file-name-internal 'PC-read-include-file-name-internal)))
  869.  
  870.  
  871. (provide 'complete)
  872.  
  873. ;;; End.
  874.